package com.qiantu.phone.widget.setting;

import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.ControlDataAirConditionerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceModeValue;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.util.TypedValue.COMPLEX_UNIT_PX;

public class DeviceSettingPanelLock extends SettingBar implements View.OnClickListener {
    private DeviceBean mDevice;
    private AppActivity mContext;
    private BottomListDialog mBottomListDialog;

    public DeviceSettingPanelLock(AppActivity context, DeviceBean deviceBean) {
        this(context, null, deviceBean);
    }

    public DeviceSettingPanelLock(AppActivity context, AttributeSet attrs, DeviceBean deviceBean) {
        this(context, attrs, 0, deviceBean);
    }

    public DeviceSettingPanelLock(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean deviceBean) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style, deviceBean);
    }

    public DeviceSettingPanelLock(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, DeviceBean deviceBean) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDevice = deviceBean;
        mContext = context;
        setLeftText(R.string.panel_lock);
        Integer lock = mDevice.getLock();
        if(lock != null){
            if(lock == 0){
                setRightText(R.string.Air_Lock_Unlocked);
            }else if(lock == 1){
                setRightText(R.string.Air_Lock_HalfLock);
            }else if(lock == 2){
                setRightText(R.string.Air_Lock_Locking);
            }
        }
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setRightTextSize(COMPLEX_UNIT_PX, DensityUtils.dip2px(context, 12));
        setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        showBottomListDialog();
    }

    private void showBottomListDialog() {
        if (mBottomListDialog == null) {
            mBottomListDialog = new BottomListDialog(mContext);
            mBottomListDialog.setDialogClickListener(new BottomListDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {

                }
            });
            ControlDataAirConditionerBean deviceBaseConfigInfo = DeviceInfoParseUtil.parseData(getContext(), mDevice.getDeviceType(), ControlDataAirConditionerBean.class);
            ControlDataAirConditionerBean.ControlData controlData = deviceBaseConfigInfo.getControlData();
            List<DeviceModeValue> lockModes = controlData.getLock();
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(mContext);
            List<String> data = new ArrayList<>();
            for(int i = 0;i < lockModes.size();i++){
                DeviceModeValue deviceModeValue = lockModes.get(i);
                int nameRes = getResources().getIdentifier(deviceModeValue.getName(), "string", getContext().getPackageName());
                if(nameRes == 0){
                    data.add(deviceModeValue.getName());
                }else{
                    data.add(getResources().getString(nameRes));
                }
            }
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    String text = listLabelAdapter.getSelected();
                    int lock = lockModes.get(position).getValue();
                    Map<String, Object> option = new HashMap<>();
                    option.put(LLDeviceUtil.LOCK, lock);
                    Map<String, Object> status = new HashMap<>();
                    status.put(LLDeviceUtil.STATUS, option);
                    LLHttpManager.operationDevice(mContext, mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(mContext){
                        @Override
                        public void onSucceed(HttpData<Void> result) {
                            super.onSucceed(result);
                            setRightText(text);
                            mDevice.setLock(lock);
                            LLDeviceDBManager.getInstance(getContext()).put(mDevice);
                            mBottomListDialog.dismiss();
                        }
                    });
                }
            });
            mBottomListDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(mContext)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomListDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomListDialog.getAdapter();
        listLabelAdapter.setSelected(getRightText().toString());
        mBottomListDialog.show();
    }
}
